package matlap.database.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import matlap.database.ConnectionManager;
import matlap.database.dao.NewsDAO;
import matlap.shared.New;

public class NewsJdbcDAO implements NewsDAO {

	Connection con = null;

	/**
	 * Turns back all of the news in New list.
	 */

	@Override
	public List<New> getNews() {
		List<New> list = new ArrayList<New>();
		String title;
		String content;
		String query = "SELECT title, content FROM news ORDER BY title";
		try {
			ConnectionManager.initConnection();
			con = ConnectionManager.createConnection();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery(query);
			int i = 0;

			while (rs.next()) {

				title = rs.getString("title");
				content = rs.getString("content");
				New n = new New(title, content);
				list.add(i, n);
				i++;

			}
		} catch (SQLException ex) {
			System.err.println(ex.getMessage());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				ConnectionManager.closeConnection(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return list;
	}

	/**
	 * Turns back all of the newa in New by adding news title.
	 */

	@Override
	public New getNewsByTitle(String title) {
		New n = new New();
		String content;
		String query = "SELECT content FROM news WHERE title='" + title + "'";
		try {
			ConnectionManager.initConnection();
			con = ConnectionManager.createConnection();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery(query);

			while (rs.next()) {
				content = rs.getString("content");
				n = new New(title, content);

			}
		} catch (SQLException ex) {
			System.err.println(ex.getMessage());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				ConnectionManager.closeConnection(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return n;
	}

	/**
	 * Inserts news INTO news table by adding New.
	 */

	@Override
	public void insertNews(New n) {
		try {
			ConnectionManager.initConnection();
			con = ConnectionManager.createConnection();
			Statement st = con.createStatement();
			String s = new String("INSERT INTO news VALUES('" + n.getTitle()
					+ "','" + n.getContent() + "')");
			st.executeUpdate(s);

		} catch (SQLException ex) {
			System.err.println(ex.getMessage());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				ConnectionManager.closeConnection(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * Updates news table by adding new.
	 */

	@Override
	public void updateNews(New n) {
		try {
			ConnectionManager.initConnection();
			con = ConnectionManager.createConnection();
			Statement st = con.createStatement();
			String s = new String("UPDATE news SET content='" + n.getContent()
					+ "' WHERE title='" + n.getTitle() + "'");
			st.executeUpdate(s);

		} catch (SQLException ex) {
			System.err.println(ex.getMessage());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				ConnectionManager.closeConnection(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * Deletes news by adding news title in new.
	 */

	@Override
	public void deleteNews(New n) {
		try {
			ConnectionManager.initConnection();
			con = ConnectionManager.createConnection();
			Statement st = con.createStatement();
			String s = new String("DELETE FROM news WHERE title='"
					+ n.getTitle() + "'");
			st.executeUpdate(s);

		} catch (SQLException ex) {
			System.err.println(ex.getMessage());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				ConnectionManager.closeConnection(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

}
